
import tensorflow as tf

checkpoint_prefix = "./train/module"

# first creat a simple graph
graph = tf.Graph()

#define a simple graph
with graph.as_default():
    x = tf.placeholder(tf.float32,shape=[],name='input')
    y = tf.Variable(initial_value=0,dtype=tf.float32,name="y_variable")
    update_y = y.assign(x)
    saver = tf.train.Saver(max_to_keep=3)
    init_op = tf.global_variables_initializer()

# train the model and save the model every 4000 iterations.
sess = tf.Session(graph=graph)
sess.run(init_op)
for i in range(1,20000):
    y_result = sess.run(update_y,feed_dict={x:i})
    if i %4000 == 0:
        saver.save(sess,checkpoint_prefix,global_step=i)
        variable_names = [v.name for v in tf.trainable_variables()]
        print(" has {} variables".format(len(variable_names)))
        for var in variable_names:
            print(var + " {},".format(sess.run(var)))
